package cn.ejiakanghu.logistics.service.impl;

import cn.com.ejiakanghu.core.exceptions.BizException;
import cn.ejiakanghu.logistics.service.ReturnEquService;
import cn.hutool.core.util.StrUtil;
import org.linlinjava.litemall.db.dao.AgedmallLgBaseInfoMapper;
import org.linlinjava.litemall.db.dao.AgedmallLgMissionMapper;
import org.linlinjava.litemall.db.dao.AgedmallLgReceiveOrderMapper;
import org.linlinjava.litemall.db.dao.LitemallOrderGoodsMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.BaseInfoLogisticsStatusErrorMessageEnum;
import org.linlinjava.litemall.db.util.LgBaseInfoStatConstant;
import org.linlinjava.litemall.db.util.LgMissionIsOverEum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：sujp
 * @date ：Created in 2021/11/15 19:02
 * @description：归还相关操作
 * @modified By：
 * @version: V1.0
 */
@Service
public class ReturnEquServiceImpl extends LogisticBusiBaseServiceImpl implements ReturnEquService {
    @Autowired
    private LogisticsBaseServiceAbstractImpl logisticsBaseServiceAbstractImpl;
    @Resource
    private AgedmallLgMissionMapper agedmallLgMissionMapper;
    @Resource
    private AgedmallLgBaseInfoMapper agedmallLgBaseInfoMapper;


    /**
     * 归还中
     *
     * @param manId
     * @param missionId
     * @param messageType
     * @param receiveType
     * @param execType
     */
    @Override
    public void returningAndPickingUp(Integer manId, Integer missionId, Integer messageType, int receiveType, int execType) {
        //更新数据
        AgedmallLgMission mission = agedmallLgMissionMapper.selectByPrimaryKey(missionId);
        AgedmallLgBaseInfo agedmallLgBaseInfo = agedmallLgBaseInfoMapper.selectByPrimaryKey(mission.getBaseInfoId().longValue());
        Integer stat = LgBaseInfoStatConstant.getNowStat(agedmallLgBaseInfo.getLgType(), messageType);
        agedmallLgBaseInfo.setLgStat(stat);
        agedmallLgBaseInfoMapper.updateByPrimaryKeySelective(agedmallLgBaseInfo);

        //新增agedmall_lg_receive_order  agedmall_lg_busi_record
        AgedmallLgReceiveOrder receiveOrder = insertReceiveOrder(agedmallLgBaseInfo, manId, receiveType, execType,mission.getId().longValue());
        insertBusiRecord(mission.getId().longValue(), receiveOrder.getId(), mission.getType(), agedmallLgBaseInfo.getOrderCode(), 1);

        //更改订单商品的状态
        updateOrderGoodsStat(agedmallLgBaseInfo.getLgContent(), stat);
    }

    /**
     * 归还完成
     *
     * @param missionId
     * @param imgs
     */
    @Override
    public boolean returnComplete(Integer missionId, List<String> imgs) {
        return logisticsBaseServiceAbstractImpl.pickingCompleteOperation(missionId, imgs, LgBaseInfoStatConstant.RETURNING, LgBaseInfoStatConstant.RETURN_COMPLETE, BaseInfoLogisticsStatusErrorMessageEnum.NOT_IN_RETURN_STATE.getMessage());

    }
}
